<?php
global $xml_element_name, $name, $xml_parser;

// Request Yahoo! REST Web Service using
// HTTP POST with curl. PHP4/PHP5
// Allows retrieval of HTTP status code for error reporting
// Author: Jason Levitt
// February 1, 2006

	error_reporting(E_ALL);

	$lat = $_REQUEST['lat'];
	$lon = $_REQUEST['lon'];
	$proximity = 30;

	$activities = '11,39,31,41,10,33';
	$version = '3';
	$api_key = 'b5d1218f5af7c6da3ba3eea65b402f43';
	$secret = '2799dd827f79fd39';
	$postargs = '&version='.urlencode($version);

// this works
//$request = 'http://www.everytrail.com/api/trip';
//$postargs .= '&trip_id='.urlencode($trip_id);

// this works
//$request = 'http://www.everytrail.com/api/user/trips';
//$postargs .= '&user_id='.urlencode($user_id);

// this works
//$request = 'http://www.everytrail.com/api/activities';

// this works too - thanks to Chris McCarty chris@globalmotion.com
	$request   = 'http://www.everytrail.com/api/trip/search';
	$postargs .= '&lat='.urlencode($lat);
	$postargs .= '&lon='.urlencode($lon);
	$postargs .= '&proximity='.urlencode($proximity);
	$postargs .= '&activities='.urlencode($activities);

	$session = curl_init($request);

// Set the POST options.
	curl_setopt ($session, CURLOPT_POST, true);
	curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
	curl_setopt ($session, CURLOPT_HEADER, true);
	curl_setopt ($session, CURLOPT_RETURNTRANSFER, true);
	curl_setopt ($session, CURLOPT_USERPWD,"$api_key:$secret"); 

// Do the POST and then close the session
	$response = curl_exec($session);
	curl_close($session);

// Get HTTP Status code from the response
	$status_code = array();
	preg_match('/\d\d\d/', $response, $status_code);

// Check for errors
	switch( $status_code[0] ) {
		case 200:
			// Success
			break;
		case 503:
			die('Your call to Yahoo Web Services failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem prevented us from returning data to you.');
			break;
		case 403:
			die('Your call to Yahoo Web Services failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');
			break;
		case 400:
			// You may want to fall through here and read the specific XML error
			die('Your call to Yahoo Web Services failed and returned an HTTP status of 400. That means:  Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
			break;
		default:
			die('Your call to Yahoo Web Services returned an unexpected HTTP status of:' . $status_code[0]);
	}

	// Get the XML from the response, bypassing the header
	if (!($xml = strstr($response, '<?xml'))) {
		$xml = null;
	}

	$xml_parser = xml_parser_create();
	xml_set_element_handler($xml_parser, "startElement", "endElement");
	xml_set_character_data_handler($xml_parser, "characterData");
	parse_xml($xml);

//
// xml parsing functions (referenced in xml setup above)
//
function parse_xml($kml){
  
	global $xml_parser, $kml_outline;
	if (!xml_parse($xml_parser, $kml))   {
		die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), 
       		xml_get_current_line_number($xml_parser)));  
	}
}


function startElement($parser, $name, $attrs) {  
  global $kml_outline, $elements, $xml_element_name, $num_placemarks, $num_elements, $num_points, $num_polygons, $num_linestrings;
  echo('<br>name: '.$name);
  while (list($key, $val) = each($attrs)) {
  	  echo "<li>attribute: $key => $val"; 
  }
}

function characterData($parser, $data) { 
	global $xml_element_name, $num_coordinates, $num_latlon;
	global $kml_outline, $min_lat,$min_lon,$max_lat,$max_lon;
	echo "<br><i>data: $data</i>";
}

function endElement($parser, $name) {
  global $xml_element_name, $kml_outline;
}
?>
